const User = require("../model/User");

// 创建一个数组存储用户名，密码，头像和学校
const usernames = ["周一", "吴二", "张三", "李四", "王五", "毛六"];
// const nikename = ["周一", "吴二", "张三", "李四", "王五", "毛六"];
// const passwords = ["123456", "abcdef", "qwerty", "iloveyou", "password"];
// const schools = ["北京大学", "清华大学", "复旦大学", "浙江大学", "南京大学"];
const telephone = [Randomtel()];
const RegTime = [getRegTime()];
const consumption = [Randomconsumption()];
const goods = [Randomgoods()];
// 定义一个函数，从一个数组中随机选择一个元素
function randomChoice(array) {
  return array[Math.floor(Math.random() * array.length)];
}
//定义一个函数，随机生成11位的数字
function Randomtel() {
  let tel = "";
  for (let i = 0; i <= 11; i++) {
    tel += String(Math.floor(Math.random() * 10));
  }
  return tel;
}
//定义一个函数，随机生成消费金额
function Randomconsumption() {
  let consumption = "0";
  for (let i = 0; i <= Math.ceil(Math.random() * 5); i++) {
    consumption += Math.ceil(Math.random() * 10);
  }
  consumption = Number(consumption);
  return consumption;
}
//定义一个函数，随机生成订单数量
function Randomgoods() {
  let consumption = "";
  for (let i = 0; i <= Math.floor(Math.random() * 2); i++) {
    consumption += Math.ceil(Math.random() * 10);
  }
  consumption = Number(consumption);
  return consumption;
}
// 定义一个函数，生成一个用户对象
function generateUser() {
  return new User({
    username: randomChoice(usernames),
    // password: randomChoice(passwords),
    // avatar: randomChoice(avatars),
    // school: randomChoice(schools),
    // nikename: randomChoice(nikename),
    telephone: randomChoice(telephone),
    RegTime: randomChoice(RegTime),
    consumption: randomChoice(consumption),
    goods: randomChoice(goods),
  });
}
//定义一个函数，获取当前事件
function getRegTime() {
  const date = new Date();

  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, "0");
  const day = String(date.getDate()).padStart(2, "0");
  const hours = String(date.getHours()).padStart(2, "0");
  const minutes = String(date.getMinutes()).padStart(2, "0");
  const seconds = String(date.getSeconds()).padStart(2, "0");

  // 格式化为 "YYYY-MM-DD HH:MM:SS" 的字符串
  const currentTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;

  return currentTime;
}

// 定义一个函数，生成一定数量的用户并保存到数据库
async function generateUsers(n) {
  for (let i = 0; i < n; i++) {
    let user = generateUser();
    await User.create(user);
  }
}
// 调用函数，生成10个用户
// generateUsers(10);
